package com.ruoyi.lbb.service.impl;

import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import com.ruoyi.common.utils.StringUtils;
import org.springframework.transaction.annotation.Transactional;
import com.ruoyi.lbb.domain.Frontslave;
import com.ruoyi.lbb.mapper.FronthostMapper;
import com.ruoyi.lbb.domain.Fronthost;
import com.ruoyi.lbb.service.IFronthostService;

import javax.annotation.Resource;

/**
 * 用户管理Service业务层处理
 * 
 * @author lbb
 * @date 2022-09-22
 */
@Service
public class FronthostServiceImpl implements IFronthostService 
{
    @Resource
    private FronthostMapper fronthostMapper;

    /**
     * 查询用户管理
     * 
     * @param uid 用户管理主键
     * @return 用户管理
     */
    @Override
    public Fronthost selectFronthostByUid(Long uid)
    {
        return fronthostMapper.selectFronthostByUid(uid);
    }

    /**
     * 查询用户管理列表
     * 
     * @param fronthost 用户管理
     * @return 用户管理
     */
    @Override
    public List<Fronthost> selectFronthostList(Fronthost fronthost)
    {
        System.out.println(fronthostMapper.selectFronthostList(fronthost));
        return fronthostMapper.selectFronthostList(fronthost);
    }

    /**
     * 新增用户管理
     * 
     * @param fronthost 用户管理
     * @return 结果
     */
    @Transactional
    @Override
    public int insertFronthost(Fronthost fronthost)
    {
        int rows = fronthostMapper.insertFronthost(fronthost);
        insertFrontslave(fronthost);
        return rows;
    }

    /**
     * 修改用户管理
     * 
     * @param fronthost 用户管理
     * @return 结果
     */
    @Transactional
    @Override
    public int updateFronthost(Fronthost fronthost)
    {
        fronthostMapper.deleteFrontslaveByUid(fronthost.getUid());
        insertFrontslave(fronthost);
        return fronthostMapper.updateFronthost(fronthost);
    }

    /**
     * 批量删除用户管理
     * 
     * @param uids 需要删除的用户管理主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteFronthostByUids(Long[] uids)
    {
        fronthostMapper.deleteFrontslaveByUids(uids);
        return fronthostMapper.deleteFronthostByUids(uids);
    }

    /**
     * 删除用户管理信息
     * 
     * @param uid 用户管理主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteFronthostByUid(Long uid)
    {
        fronthostMapper.deleteFrontslaveByUid(uid);
        return fronthostMapper.deleteFronthostByUid(uid);
    }

    /**
     * 新增usermanage信息
     * 
     * @param fronthost 用户管理对象
     */
    public void insertFrontslave(Fronthost fronthost)
    {
        List<Frontslave> frontslaveList = fronthost.getFrontslaveList();
        Long uid = fronthost.getUid();
        if (StringUtils.isNotNull(frontslaveList))
        {
            List<Frontslave> list = new ArrayList<Frontslave>();
            for (Frontslave frontslave : frontslaveList)
            {
                frontslave.setUid(uid);
                list.add(frontslave);
            }
            if (list.size() > 0)
            {
                fronthostMapper.batchFrontslave(list);
            }
        }
    }
}
